package com.jhx.common.framework;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.AnimRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jhx.common.R;
import com.jhx.common.utils.DisplayUtil;
import com.jhx.common.widget.CommonToolbar;
import com.jhx.common.widget.LoadingDialog;

/**
 * fragment基类
 *
 * @author jack 2017-05-17 17:54
 */
public class QuickBaseFragment extends Fragment {
    private   LinearLayout  mContentView;
    protected CommonToolbar mToolbar;
    protected TextView      mMenuText;
    protected LoadingDialog mLoadingDialog;
    protected final String  TAG = getClass().getName();

    @Nullable
    @Override
    final public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = new LinearLayout(getActivity());
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.setOrientation(LinearLayout.VERTICAL);
        mContentView.setLayoutParams(lp);
        if(showToolbar()) {
            mToolbar = new CommonToolbar(getActivity());
            ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams
                    (ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtil.dip2px(getActivity(), 50));
            mToolbar.setLayoutParams(params);
            mToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            mToolbar.setPopupTheme(R.style.ToolbarPopupTheme);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onNavigationClick(v);
                }
            });
            mContentView.addView(mToolbar);
            setNavigationIcon(R.mipmap.icon_back_white);
        }
        View bindView = bindView(inflater, container, savedInstanceState);
        if(bindView != null) {
            mContentView.addView(bindView);
        }
        setHasOptionsMenu(true);
        return mContentView;
    }

    public View bindView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        throw new RuntimeException("请初始化view");
    }

    protected boolean showToolbar() {
        return false;
    }

    public void setTitle(String title) {
        if(TextUtils.isEmpty(title) || mToolbar == null) {
            return;
        }
        mToolbar.setTitle(title);
        if(mToolbar.getTitleView() != null) {
            setTitleView(mToolbar.getTitleView());
        }
    }

    public void setTitleView(TextView textView) {
        if(textView != null) {
            textView.setTextColor(Color.WHITE);
            textView.setTextSize(20);
        }
    }

    public void setNavigationIcon(@DrawableRes int resId) {
        Drawable drawable = getResources().getDrawable(resId);
        setNavigationIcon(drawable);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    public void setNavigationIcon(Drawable drawable) {
        if(mToolbar != null && drawable != null) {
            mToolbar.setNavigationIcon(drawable);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onNavigationClick(view);
                }
            });
        }
    }

    protected void onNavigationClick(View view) {
        onBackPressed();
    }

    public void push(Class<? extends QuickBaseFragment> fragment, Bundle bundle) {
        ((QuickBaseActivity)getActivity()).push(fragment, bundle);
    }

    public void push(@AnimRes int enterAnim,
                     @AnimRes int exitAnim,
                     @AnimRes int popEnterAnim,
                     @AnimRes int popExitAnim,
                     Class<? extends QuickBaseFragment> fragment,
                     Bundle bundle) {
        ((QuickBaseActivity)getActivity()).push(enterAnim,
                exitAnim,
                popEnterAnim,
                popExitAnim,
                true,
                fragment,
                bundle);
    }

    public void switchTo(Class<? extends Fragment> target, Bundle bundle) {
        if(getActivity() != null) {
            ((QuickBaseActivity)getActivity()).switchTo(target, bundle);
        }
    }

    public void pop() {
        if(getActivity() != null) {
            ((QuickBaseActivity)getActivity()).pop();
        }
    }

    public Boolean interceptionBack() {
        return false;
    }

    public void onBackPressed() {
        pop();
    }

    protected void showLoadingDialog() {
        showLoadingDialog("");
    }

    protected void showLoadingDialog(String msg) {
        if(getActivity().isFinishing()) {
            return;
        }
        if(mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(getActivity());
        }
        if(!TextUtils.isEmpty(msg)) {
            mLoadingDialog.setTitle(msg);
        }
        mLoadingDialog.dismiss();
        mLoadingDialog.show();
    }

    protected void hideLoadingDialog() {
        if(mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    protected void setMenuText(String text) {
        if(mMenuText == null) {
            mMenuText = new TextView(getActivity());
            ViewGroup.MarginLayoutParams textParams = new ViewGroup.MarginLayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            mMenuText.setLayoutParams(textParams);
            mMenuText.setTextColor(Color.WHITE);
            mMenuText.setPadding(15, 15, 15, 15);
            if(mToolbar != null) {
                mToolbar.setMenuView(mMenuText);
                mMenuText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onMenuClick(v);
                    }
                });
            }
        }
        mMenuText.setText(text);
    }


    public void onMenuClick(View view) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

}
